home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / jav503.zip / BOUNCEAN.JAV < prev    next >
Text File  |  1996-06-04  |  2KB  |  119 lines

  1. // -[KeepHeading]-
  2.  
  3.  
  4. // -[Copyright]-
  5.  
  6. /**
  7.  * (c) Step Ahead Software Pty Ltd 1996. All rights reserved. Registered
  8.  * of JAVelin may use this applet in their web pages.
  9.  */
  10. import java.lang.*;
  11.  
  12.  
  13. // -[KeepBeforeClass]-
  14. import    java.awt.*;
  15.  
  16.  
  17. // -[Class]-
  18.  
  19. /**
  20.  * @jTitle           BounceAnimation
  21.  * @jAuthor          Chris Colman
  22.  * @jOverridability  can be overridden
  23.  * @jDescription
  24.  * Class for objects that can bounce off the left and right boundaries.
  25.  * 
  26.  * @see              
  27.  */
  28. public 
  29. class BounceAnimation
  30. {
  31. // -[KeepWithinClass]-
  32.  
  33.  
  34. // -[Fields]-
  35.  
  36.  
  37.  
  38. /**
  39.  * Color of the object.
  40.  */
  41. protected Color color;
  42.  
  43.  
  44.  
  45. /**
  46.  * Current direction of object: -1 is to the left, 1 is to the right.
  47.  */
  48. protected int direction;
  49.  
  50.  
  51.  
  52. /**
  53.  * The text displayed by the object as it moves.
  54.  */
  55. protected String s;
  56.  
  57.  
  58.  
  59. /**
  60.  * Current speed of object.
  61.  */
  62. protected int speed;
  63.  
  64.  
  65.  
  66. /**
  67.  * x coorinate of current position.
  68.  */
  69. protected int x;
  70.  
  71.  
  72.  
  73. /**
  74.  * y coordinate of current position.
  75.  */
  76. protected int y;
  77.  
  78.  
  79. // -[Methods]-
  80.  
  81.  
  82. /**
  83.  * Constructs a bouncing object with the given text (string), direction
  84.  * (-1 left, 1 right), color, initial position and speed.
  85.  */
  86. public BounceAnimation(String s, int direction, Color color, int x, int y, int speed) {
  87.         this.s = s;
  88.         this.direction = direction;
  89.         this.color = color;
  90.         this.x = x;
  91.         this.y = y;
  92.         this.speed = speed;
  93.  }
  94.  
  95. public void advance(int dispWidth) {
  96.  
  97.         if ( x > dispWidth )
  98.                 direction = -1;
  99.         else if ( x < 0 )
  100.                 direction = 1;
  101.  
  102.         // Adjust horizontals
  103.         switch ( direction )
  104.         {
  105.                 case 1:
  106.                         x += speed;
  107.                         break;
  108.                 case -1:
  109.                         x -= speed;
  110.         }
  111.  }
  112.  
  113. public void paint(Graphics g) {
  114.         Color oldColor = g.getColor();
  115.         g.setColor(color);
  116.         g.drawString(s, x, y);
  117.         g.setColor(oldColor);
  118.  }
  119. }